package com.peaksport.framework.metadata.filter;

import com.peaksport.framework.exception.PKDevException;
import com.peaksport.framework.metadata.venum.CompareTypeEnum;
import com.peaksport.framework.metadata.venum.MergeLogicEnum;
import com.peaksport.framework.util.PKPublicUtils;

/**
 * 
 * 
 *
 * @author jinbin_guo 2019年4月15日
 *
 */
public class FilterInfo {

    private static final String SPACE = PKPublicUtils.SPACE;

    private FilterItemCollection filterItemCol = new FilterItemCollection();
    private String maskString;

    private FilterInfo mergeFilterInfo =  null;
    private MergeLogicEnum mergeLogic;


    public void addFilterItemInfo(FilterItemInfo filterItemInfo) {
        filterItemCol.addFilterItemInfo(filterItemInfo);
    }
    public void addFilterItemInfo(String compareExpr, Object compareValue, CompareTypeEnum compareType) {
        FilterItemInfo filterItemInfo = new FilterItemInfo(compareExpr,compareValue,compareType);
        addFilterItemInfo(filterItemInfo);
    }
    public void addFilterItemInfo(String compareExpr, Object compareValue) {
        addFilterItemInfo(compareExpr,compareValue,CompareTypeEnum.EQUALS);
    }

    public FilterItemCollection getFilterItemCollection() {
        return filterItemCol;
    }
    public void setFilterItemCollection(FilterItemCollection filterItemCol) {
        this.filterItemCol = filterItemCol;
    }

    public void addMergeFilterInfo(FilterInfo mergeFilterInfo, MergeLogicEnum mergeLogic) {
        this.mergeFilterInfo = mergeFilterInfo;
        this.mergeLogic = mergeLogic;
    }

    public FilterInfo getMergeFilterInfo() {
        return mergeFilterInfo;
    }

    public void setMaskString(String maskString) {
        this.maskString = maskString;
    }

    public boolean isEmpty() {
        return filterItemCol.isEmpty() && mergeFilterInfo == null;
    }

    public String toSQL() {
        StringBuffer sql = new StringBuffer();
        if (PKPublicUtils.isEmpty(maskString)) {
              for (int i = 0, len = filterItemCol.size(); i < len; i++) {
                  FilterItemInfo filterItemInfo = filterItemCol.get(i);
                  sql.append(filterItemInfo.toSQL());
                  if (i != len -1) sql.append(" and ");
              }
        } else {
            int maskIndex = 0;
            String s1 = String.valueOf((char)1);
            String maskSQL = maskString;
            while (maskSQL.indexOf("#") != -1) {
                String key = "#"+ maskIndex;
                if (maskIndex >= filterItemCol.size()) {
                    throw PKDevException.throwException("maskString格式有误:大小超出");
                }
                FilterItemInfo filterItemInfo = filterItemCol.get(maskIndex);
                String filter = filterItemInfo.toSQL().replace("#",s1);
                maskSQL = maskSQL.replace(key,filter + SPACE);
                maskIndex ++;
            }
            maskSQL = maskSQL.replace(s1,"#");
            if (maskIndex != filterItemCol.size()) {
                throw PKDevException.throwException("maskString格式有误:大小不一致");
            }
            sql.append(maskSQL);
        }

        if (mergeFilterInfo != null) {
            if (sql.length() == 0) sql.append(mergeFilterInfo.toSQL());
            else sql.insert(0,"(").append(") ").append(mergeLogic.getValue()).append(" (").append(mergeFilterInfo.toSQL()).append(")");
        }

        return sql.toString();
    }

}
